﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using XWG.JWBST.JWBSTEntity;
using XWG.JWBST.JWBSTBLL;
using XWG.Framework.MSException;
using JWBST.CommonClass;

namespace XWG.JWBST.JWBSTManagementApp.XSJB
{
    public partial class CrimecluesApply : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Page.RegisterStartupScript("提示", "<script language=javascript>window.history.go(1);</script>");
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                labMsg.Text = "";
                if (txtyanzhengma.Value.FilteHtml().ToString() != Session["Crimeclues"].ToString())
                {
                    labMsg.Text = "请填写正确的验证码";
                    return;
                }


                TbBstCrimeclues mTbBstCrimeclues = new TbBstCrimeclues();
                mTbBstCrimeclues.Id = Guid.NewGuid().ToString(); //主键
                mTbBstCrimeclues.Name = texInformername.Value;
                mTbBstCrimeclues.Tel = texInformertel.Value;
                mTbBstCrimeclues.Email = texEmail.Value;
                mTbBstCrimeclues.Address = texAddress.Value;
                mTbBstCrimeclues.Content = texMsgcontent.Value.FilteHtml(); //短信内容                   
                mTbBstCrimeclues.Createdate = DateTime.Now;
                mTbBstCrimeclues.Movesign = "1"; //同步标志
                mTbBstCrimeclues.Status = "0"; //0 未推送  1已推送 2 已删除

                mTbBstCrimeclues.Ipaddress = GetWebClientIp(); //举报人ip地址

                var mResultModel = TbBstCrimecluesBLL.Insert(mTbBstCrimeclues, "", false); //新建操作      
                if (mResultModel.ISSucceed)
                {

                    clertText();
                    Response.Buffer = true;
                    Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
                    Response.Expires = 0;
                    Response.CacheControl = "no-cache";
                    Page.RegisterStartupScript("提示", "<script language=javascript>alert(\"您的诉求已受理，公安机关会及时与您联系。\");</script>");
                }
                else
                {
                    Page.RegisterStartupScript("提示", "<script language=javascript>alert(\"提交失败\")</script>");
                }

            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                Page.RegisterStartupScript("提示", "<script language=javascript>alert(\"提交失败\")</script>");
                //mResultModel.ErrorCode = -9999;
                //mResultModel.ISSucceed = false;
                //context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #region 获取web客户端ip
        /// <summary>
        /// 获取web客户端ip
        /// </summary>
        /// <returns></returns>
        public static string GetWebClientIp()
        {

            string userIP = "未获取用户IP";

            try
            {
                if (System.Web.HttpContext.Current == null
            || System.Web.HttpContext.Current.Request == null
            || System.Web.HttpContext.Current.Request.ServerVariables == null)
                    return "";

                string CustomerIP = "";

                //CDN加速后取到的IP simone 090805
                CustomerIP = System.Web.HttpContext.Current.Request.Headers["Cdn-Src-Ip"];
                if (!string.IsNullOrEmpty(CustomerIP))
                {
                    return CustomerIP;
                }

                CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];


                if (!String.IsNullOrEmpty(CustomerIP))
                    return CustomerIP;

                if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
                {
                    CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                    if (CustomerIP == null)
                        CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                }
                else
                {
                    CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

                }

                if (string.Compare(CustomerIP, "unknown", true) == 0)
                    return System.Web.HttpContext.Current.Request.UserHostAddress;
                return CustomerIP;
            }
            catch { }

            return userIP;

        }
        #endregion

        public void clertText()
        {
            texEmail.Value = "";

            texMsgcontent.Value = "";

            texInformername.Value = "";
            texInformertel.Value = "";
            texAddress.Value = "";

            txtyanzhengma.Value = "";
        }
    }
}